package com.ruoyi.web.controller.mall;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUserUtils;
import com.ruoyi.mall.domain.MallShoppingCart;
import com.ruoyi.mall.service.MallShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车Controller
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Api(tags = "购物车管理")
@RestController
@RequestMapping("/mall/cart")
public class MallShoppingCartController extends BaseController {
    @Autowired
    private MallShoppingCartService mallShoppingCartService;

    /**
     * 查询购物车列表
     */
    @ApiOperation("购物车列表")
    @PostMapping("/list")
    public TableDataInfo list(MallShoppingCart mallShoppingCart) {
        startPage();
        mallShoppingCart.setUserId(SecurityUserUtils.getUserId());
        List<MallShoppingCart> list = mallShoppingCartService.selectMallShoppingCartList(mallShoppingCart);
        return getDataTable(list);
    }

    /**
     * 加入购物车
     */
    @ApiOperation("加入购物车")
    @Log(title = "加入购物车", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody MallShoppingCart mallShoppingCart) {
        mallShoppingCart.setUserId(SecurityUserUtils.getUserId());
        return toAjax(mallShoppingCartService.insertMallShoppingCart(mallShoppingCart));
    }

    /**
     * 修改购物车
     */
    @ApiOperation("修改购物车")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody MallShoppingCart mallShoppingCart) {
        return toAjax(mallShoppingCartService.updateMallShoppingCart(mallShoppingCart));
    }

    /**
     * 删除购物车
     */
    @ApiOperation("删除购物车")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @PostMapping("/{cartIds}")
    public AjaxResult remove(@PathVariable Long[] cartIds) {
        return toAjax(mallShoppingCartService.deleteMallShoppingCartByCartIds(cartIds));
    }
}
